package com.example.xinhuazidian

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.example.xinhuazidian.utils.JsonFileUtil
import com.example.xinhuazidian.adapter.TSSBS2Adapter
import com.example.xinhuazidian.adapter.TSSBS2Adapter.OnRecyclerItem2ClickListener
import com.example.xinhuazidian.adapter.TSSBSAdapter
import com.example.xinhuazidian.bean.TSSBS
import com.example.xinhuazidian.databinding.ActivityTangshisanbaiBinding
import com.google.gson.Gson
import com.gyf.immersionbar.ImmersionBar

/**
 * 唐诗三百首
 */
class TangshisanbaiActivity : AppCompatActivity(), TSSBSAdapter.OnRecyclerItemClickListener {

    private lateinit var binding: ActivityTangshisanbaiBinding
    private var tssbsAdapter: TSSBSAdapter? = null
    private var str: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityTangshisanbaiBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initView()
        initData()
    }

    private fun initData() {
        tssbsAdapter?.setRecyclerItemClickListener(this)
    }

    private fun initView() {
        //沉浸状态栏
        ImmersionBar.with(this)
            .statusBarDarkFont(true)//状态栏字体是深色，不写默认为亮色
            .init()

        load()
    }

    @SuppressLint("SetTextI18n")
    private fun load() {
        str = JsonFileUtil.loadFile(this, "tangshisanbaishou.json")

        val bean = Gson().fromJson(str, TSSBS::class.java)
        initAdapter(bean.content)

        beanX = bean.content[0].content
        initAdapter2(beanX)

        binding.tvTitle.text = bean.title
        binding.tvNum.text = "${beanX.size}首"
    }

    private fun initAdapter(list: MutableList<TSSBS.ContentBeanX>) {
        val staggeredGridLayoutManager =
            StaggeredGridLayoutManager(1, LinearLayoutManager.HORIZONTAL)
        binding.rv.layoutManager = staggeredGridLayoutManager

        //加载适配器
        tssbsAdapter = TSSBSAdapter(this, list)
        binding.rv.adapter = tssbsAdapter
    }

    private fun initAdapter2(list: MutableList<TSSBS.ContentBeanX.ContentBean>) {
        val staggeredGridLayoutManager = StaggeredGridLayoutManager(1, LinearLayoutManager.VERTICAL)
        binding.rv2.layoutManager = staggeredGridLayoutManager

        //加载适配器
        val tssbs2Adapter = TSSBS2Adapter(list)
        binding.rv2.adapter = tssbs2Adapter

        tssbs2Adapter.setRecyclerItem2ClickListener(object : OnRecyclerItem2ClickListener {
            override fun onRecyclerItem2Click(position: Int) {
                intent =
                    Intent(this@TangshisanbaiActivity, TangshisanbaiXiangqingActivity::class.java)
                intent.putExtra("bean", Gson().toJson(beanX[position]))
                startActivity(intent)
            }
        })
    }

    private lateinit var beanX: MutableList<TSSBS.ContentBeanX.ContentBean>

    @SuppressLint("SetTextI18n", "NotifyDataSetChanged")
    override fun onRecyclerItemClick(position: Int) {
        val bean = Gson().fromJson(str, TSSBS::class.java)
        beanX = bean.content[position].content
        initAdapter2(beanX)

        binding.tvNum.text = "${beanX.size}首"
    }
}